package com.coordinate.services;

import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;

public class CoordMailService {

	
	public static int
	send(String pFrom,
		 String pTo,
		 String pSubject,
		 String pContentType,
		 String pContent)
	{
		int lResult = 0;
		
		Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "hello, how are you ?";

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(pFrom, "Admin"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress(pTo, "Mr. User"));
            //msg.addRecipient(Message.RecipientType.CC,
             //       new InternetAddress("watchersparadise@gmail.com", "Mr. User"));
            msg.addRecipient(Message.RecipientType.CC,
                    new InternetAddress("simith@ctg.ae", "Mr. User"));
            msg.setSubject(pSubject);
           /* msg.setContent("<html><body bgcolor=\"lightblue\"><h2>This is an e-mail from the Department of Finance</h2>" +
            		"<embed src=http://www.devicemantra.com/coordinate/videos/nike.3gp>" +
            		"<font color=red> Hello again</font>" +
            		
            		"</body></html>",
            		"text/html");*/
            Multipart mp = new MimeMultipart();

            MimeBodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
            "<html xmlns=\"http://www.w3.org/1999/xhtml\">" +
            "<h2>Department of Education</h2><body bgcolor=\"yellow\">" +
         		//"<embed src=http://www.devicemantra.com/coordinate/videos/nike.3gp>" +
            "<div align=center><img src=\"http://egov-innovations.appspot.com/logo_govt.jpg\"></div>"+
            		"</body></html>", "text/html");
            mp.addBodyPart(htmlPart);
            Multipart mp1 = new MimeMultipart();

            MimeBodyPart textPart = new MimeBodyPart();
            textPart.setContent("This is an e-mail from the Department of Finance",
            	 "text/plain");
            mp.addBodyPart(textPart);

            msg.setContent(mp);
           Transport.send(msg);
    
        } catch (AddressException e) {
            e.printStackTrace();
            lResult = -1;
        } catch (MessagingException e) {
        	e.printStackTrace();
        	 lResult = -1;
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        	 lResult = -1;
        }
        
        if(0 == lResult)
        {
        	System.out.println("Sending mail now ! - SUCCESS");
        }
        else
        {
        	System.out.println("Sending mail now ! - FAILURE");
        }
		return lResult;
	}
	
}
